import { getToken, removeToken, setTime, setToken } from '@/utils/auth'
import { getInfo, getInfoById, login } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, payload) {
      state.token = payload
      setToken(payload)
      getToken()
    },
    delToken(state, payload) {
      state.token = ''
      removeToken(payload)
    },
    delUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async userLogin(context, payload) {
      const res = await login(payload)
      context.commit('setToken', res.data.data)
      setTime()
    },
    async getUserInfo(context, payload) {
      const res = await getInfo()
      const info = await getInfoById(res.data.data.userId)
      context.state.userInfo = { ...res.data.data, ...info.data.data }
      console.log('用户基本信息:', context.state.userInfo)
    },
    async logout(context) {
      context.commit('delToken')
      context.commit('delUserInfo')
    }
  }
}
